package com.lagou.server;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

/**
 * 把请求信息封装为request对象（根据InputSteam输入流封装）
 */
public class Request {

    private String method;//请求方式
    private String url;//例如 index.html
    private InputStream inputStream;

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public Request() {
    }

    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;

        int count = 0;
        while (count == 0){
            count = inputStream.available();
        }
        byte[] bytes = new byte[count];

        inputStream.read(bytes);

        String inputStr = new String(bytes, Charset.forName("UTF-8"));

        String firstLine = inputStr.split("\\n")[0];

        String[] s = firstLine.split(" ");

        this.method = s[0];
        this.url = s[1];
    }
}
